import { get, post, put, del } from '@/utils/request';

/**
 * 日志管理API服务
 */

// ==================== 操作日志 ====================

/**
 * 获取操作日志列表
 */
export async function getOperationLogList(params: LOGS.OperationLogListParams): Promise<LOGS.ApiResponse<{ list: LOGS.OperationLogModel[]; pagination: LOGS.PaginationInfo }>> {
  return get('/admin/logs/operations', params);
}

/**
 * 获取操作日志详情
 */
export async function getOperationLogDetail(id: number): Promise<LOGS.ApiResponse<LOGS.OperationLogModel>> {
  return get(`/admin/logs/operations/${id}`);
}

/**
 * 导出操作日志
 */
export async function exportOperationLogs(params: LOGS.LogExportParams): Promise<Blob> {
  return get('/admin/logs/operations/export', params, { responseType: 'blob' });
}

/**
 * 获取操作日志统计
 */
export async function getOperationLogStatistics(params?: { start_date?: string; end_date?: string }): Promise<LOGS.ApiResponse<any>> {
  return get('/admin/logs/operations/statistics', params);
}

/**
 * 清理操作日志
 */
export async function cleanupOperationLogs(params: LOGS.LogCleanupParams): Promise<LOGS.ApiResponse<{ deleted_count: number }>> {
  return post('/admin/logs/operations/cleanup', params);
}

// ==================== 系统日志 ====================

/**
 * 获取系统日志列表
 */
export async function getSystemLogList(params: LOGS.SystemLogListParams): Promise<LOGS.ApiResponse<{ list: LOGS.SystemLogModel[]; pagination: LOGS.PaginationInfo }>> {
  return get('/admin/logs/system', params);
}

/**
 * 获取系统日志详情
 */
export async function getSystemLogDetail(id: number): Promise<LOGS.ApiResponse<LOGS.SystemLogModel>> {
  return get(`/admin/logs/system/${id}`);
}

/**
 * 导出系统日志
 */
export async function exportSystemLogs(params: LOGS.LogExportParams): Promise<Blob> {
  return get('/admin/logs/system/export', params, { responseType: 'blob' });
}

/**
 * 获取系统日志统计
 */
export async function getSystemLogStatistics(params?: { start_date?: string; end_date?: string }): Promise<LOGS.ApiResponse<any>> {
  return get('/admin/logs/system/statistics', params);
}

/**
 * 清理系统日志
 */
export async function cleanupSystemLogs(params: LOGS.LogCleanupParams): Promise<LOGS.ApiResponse<{ deleted_count: number }>> {
  return post('/admin/logs/system/cleanup', params);
}

/**
 * 获取系统日志级别分布
 */
export async function getSystemLogLevelDistribution(params?: { start_date?: string; end_date?: string }): Promise<LOGS.ApiResponse<any>> {
  return get('/admin/logs/system/level-distribution', params);
}

/**
 * 获取系统日志分类分布
 */
export async function getSystemLogCategoryDistribution(params?: { start_date?: string; end_date?: string }): Promise<LOGS.ApiResponse<any>> {
  return get('/admin/logs/system/category-distribution', params);
}

// ==================== 错误日志 ====================

/**
 * 获取错误日志列表
 */
export async function getErrorLogList(params: LOGS.ErrorLogListParams): Promise<LOGS.ApiResponse<{ list: LOGS.ErrorLogModel[]; pagination: LOGS.PaginationInfo }>> {
  return request('/admin/logs/errors', {
    method: 'GET',
    params,
  });
}

/**
 * 获取错误日志详情
 */
export async function getErrorLogDetail(id: number): Promise<LOGS.ApiResponse<LOGS.ErrorLogModel>> {
  return request(`/admin/logs/errors/${id}`, {
    method: 'GET',
  });
}

/**
 * 标记错误日志为已解决
 */
export async function resolveErrorLogs(params: LOGS.ResolveErrorLogParams): Promise<LOGS.ApiResponse<{ resolved_count: number }>> {
  return request('/admin/logs/errors/resolve', {
    method: 'POST',
    data: params,
  });
}

/**
 * 导出错误日志
 */
export async function exportErrorLogs(params: LOGS.LogExportParams): Promise<Blob> {
  return request('/admin/logs/errors/export', {
    method: 'GET',
    params,
    responseType: 'blob',
  });
}

/**
 * 获取错误日志统计
 */
export async function getErrorLogStatistics(params?: { start_date?: string; end_date?: string }): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/errors/statistics', {
    method: 'GET',
    params,
  });
}

/**
 * 清理错误日志
 */
export async function cleanupErrorLogs(params: LOGS.LogCleanupParams): Promise<LOGS.ApiResponse<{ deleted_count: number }>> {
  return request('/admin/logs/errors/cleanup', {
    method: 'POST',
    data: params,
  });
}

/**
 * 获取错误类型分布
 */
export async function getErrorTypeDistribution(params?: { start_date?: string; end_date?: string }): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/errors/type-distribution', {
    method: 'GET',
    params,
  });
}

/**
 * 获取错误趋势
 */
export async function getErrorTrend(params: { start_date: string; end_date: string; granularity?: 'hour' | 'day' }): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/errors/trend', {
    method: 'GET',
    params,
  });
}

/**
 * 获取热门错误
 */
export async function getTopErrors(params?: { start_date?: string; end_date?: string; limit?: number }): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/errors/top-errors', {
    method: 'GET',
    params,
  });
}

// ==================== 日志分析 ====================

/**
 * 获取日志统计概览
 */
export async function getLogStatistics(): Promise<LOGS.ApiResponse<LOGS.LogStatistics>> {
  return request('/admin/logs/statistics', {
    method: 'GET',
  });
}

/**
 * 执行日志分析
 */
export async function analyzeLogData(params: {
  log_types: ('operation' | 'system' | 'error')[];
  start_date: string;
  end_date: string;
  analysis_type: 'trend' | 'anomaly' | 'performance' | 'security';
}): Promise<LOGS.ApiResponse<LOGS.LogAnalysisResult>> {
  return request('/admin/logs/analyze', {
    method: 'POST',
    data: params,
  });
}

/**
 * 获取日志分析报告
 */
export async function getLogAnalysisReport(params: {
  start_date: string;
  end_date: string;
  include_recommendations?: boolean;
}): Promise<LOGS.ApiResponse<LOGS.LogAnalysisResult>> {
  return request('/admin/logs/analysis-report', {
    method: 'GET',
    params,
  });
}

/**
 * 搜索日志
 */
export async function searchLogs(params: {
  log_type: 'operation' | 'system' | 'error';
  keyword: string;
  start_date?: string;
  end_date?: string;
  filters?: Record<string, any>;
  page?: number;
  limit?: number;
}): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/search', {
    method: 'POST',
    data: params,
  });
}

// ==================== 日志配置 ====================

/**
 * 获取日志配置
 */
export async function getLogConfig(): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/config', {
    method: 'GET',
  });
}

/**
 * 更新日志配置
 */
export async function updateLogConfig(config: {
  retention_days?: Record<string, number>;
  log_levels?: Record<string, string[]>;
  auto_cleanup?: boolean;
  alert_rules?: any[];
}): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/config', {
    method: 'PUT',
    data: config,
  });
}

/**
 * 获取日志存储信息
 */
export async function getLogStorageInfo(): Promise<LOGS.ApiResponse<{
  total_size: number;
  operation_logs_size: number;
  system_logs_size: number;
  error_logs_size: number;
  oldest_log_date: string;
  newest_log_date: string;
}>> {
  return request('/admin/logs/storage-info', {
    method: 'GET',
  });
}

// ==================== 日志监控和告警 ====================

/**
 * 获取日志告警规则
 */
export async function getLogAlertRules(): Promise<LOGS.ApiResponse<any[]>> {
  return request('/admin/logs/alert-rules', {
    method: 'GET',
  });
}

/**
 * 创建日志告警规则
 */
export async function createLogAlertRule(rule: {
  name: string;
  description?: string;
  log_type: 'operation' | 'system' | 'error';
  condition: any;
  threshold: number;
  time_window: number;
  notification_channels: string[];
  is_enabled: boolean;
}): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/alert-rules', {
    method: 'POST',
    data: rule,
  });
}

/**
 * 更新日志告警规则
 */
export async function updateLogAlertRule(id: number, rule: any): Promise<LOGS.ApiResponse<any>> {
  return request(`/admin/logs/alert-rules/${id}`, {
    method: 'PUT',
    data: rule,
  });
}

/**
 * 删除日志告警规则
 */
export async function deleteLogAlertRule(id: number): Promise<LOGS.ApiResponse<null>> {
  return request(`/admin/logs/alert-rules/${id}`, {
    method: 'DELETE',
  });
}

/**
 * 获取日志告警历史
 */
export async function getLogAlertHistory(params?: { page?: number; limit?: number; start_date?: string; end_date?: string }): Promise<LOGS.ApiResponse<any>> {
  return request('/admin/logs/alert-history', {
    method: 'GET',
    params,
  });
}
